package com.nuanshui.heatedloan.controller.display.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.facade.CustomerIdCardInfoService;
import com.nuanshui.heatedloan.credit.form.CustomerIdCardInfoForm;
import com.nuanshui.heatedloan.credit.vo.CustomerIdCardInfoVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangxw on 2017/6/27.
 */


@RequestMapping("credit/idCard/")
@RestController
public class CustomerIdCardInfoController extends BaseController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerIdCardInfoService customerIdCardInfoService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String addCustomerIdCardInfo(String customerIdCardInfoMsg){
        if(SysStringUtils.isEmpty(customerIdCardInfoMsg)){
            return error("请传入参数");
        }
        String userId= getCurrentUser().getId();
        CustomerIdCardInfoForm  customerIdCardInfoFormNew=null;
        //身份证相关信息的新增、修改
        CustomerIdCardInfoForm  customerIdCardInfoForm=JSON.parseObject(customerIdCardInfoMsg,CustomerIdCardInfoForm.class);

        //对身份证的信息进行解密
        //String idCard = DESC.decrypt3DES(customerIdCardInfoForm.getIdCard());
        customerIdCardInfoForm.setUserId(userId);
        customerIdCardInfoForm.setStatus(1);
        String mixIdCard=customerIdCardInfoForm.getIdCard();
        String idCard= DESC.decrypt3DES(mixIdCard);
        List<CustomerIdCardInfoVO> customerIdCardInfoVOs=new ArrayList<CustomerIdCardInfoVO>();
        customerIdCardInfoVOs=customerIdCardInfoService.getAllCustomerIdCardInfoVO();
        for(CustomerIdCardInfoVO customerIdCardInfoVO:customerIdCardInfoVOs){
            //新增
            String mixOldIdCard=customerIdCardInfoVO.getIdCard();
            String oldIdCard=DESC.decrypt3DES(mixOldIdCard);
            if(idCard.equalsIgnoreCase(oldIdCard)&&SysStringUtils.isEmpty(customerIdCardInfoForm.getId())||
                    (idCard.equalsIgnoreCase(oldIdCard)&&!SysStringUtils.isEmpty(customerIdCardInfoForm.getId())
                            &&!customerIdCardInfoVO.getId().equals(customerIdCardInfoForm.getId()))){
                return error("同一个身份证号请不要重复添加哦~~");
            }
        }
        if(customerIdCardInfoForm.getId()!=null){
            customerIdCardInfoFormNew=customerIdCardInfoService.updateCustomerIdCardInfoVO(customerIdCardInfoForm);
        }else{
            customerIdCardInfoFormNew=customerIdCardInfoService.saveCustomerIdCardInfoVO(customerIdCardInfoForm);
        }
        return success(customerIdCardInfoFormNew);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public String deleteCustomerIdCardInfoById(String id){
        customerIdCardInfoService.deleteCustomerIdCardInfoVO(id);
        return success();
    }

    @RequestMapping(value = "getInfoById",method = RequestMethod.POST)
    public String getCustomerIdCardInfoById(String id){
        CustomerIdCardInfoVO customerIdCardInfoVO=customerIdCardInfoService.loadVO(id);
        return success(customerIdCardInfoVO);
    }

    @RequestMapping(value = "getInfoByUserId",method = RequestMethod.POST)
    public String getCustomerIdCardInfoByUserId(){
        String userId= getCurrentUser().getId();
        CustomerIdCardInfoVO customerIdCardInfoVO=customerIdCardInfoService.loadByUserId(userId);
        return success(customerIdCardInfoVO);
    }

}
